package com.kd.juhaowan.ui.adapter;

import android.content.Context;
import android.content.res.Resources;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;


import com.kd.juhaowan.ui.listener.OnItemClickListener;

import butterknife.ButterKnife;

public class BaseViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    private OnItemClickListener onItemClickListener;
    private final View itemView;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public OnItemClickListener getOnItemClickListener() {
        return onItemClickListener;
    }

    public BaseViewHolder(@NonNull View itemView) {
        this(itemView, true);
    }

    public BaseViewHolder(@NonNull View itemView, boolean clickable) {
        super(itemView);
        ButterKnife.bind(this, itemView);
        if(itemView != null && clickable){
            itemView.setOnClickListener(this);
        }
        this.itemView = itemView;
    }

    @Override
    public void onClick(View v) {
        if(onItemClickListener != null){
            onItemClickListener.onItemClick(this, this.itemView, getLayoutPosition());
        }
    }

    public View getItemView(){
        return this.itemView;
    }

    public String getString(int resourceId, Object... formatArgs){
        Context context = getItemView().getContext();
        return context.getString(resourceId, formatArgs);
    }

    public String[] getStringArray(int resourceId){
        Context context = getItemView().getContext();
        Resources resources = context.getResources();
        return resources.getStringArray(resourceId);
    }

    public Context getContext(){
        if(itemView != null){
            return itemView.getContext();
        }
        return null;
    }
}
